package com.gitee.ywj1352.leecode;

public class JiOuShu {
    private static Object lock = new Object();
    //要求两个线程之间交替打印 奇偶数
    private static volatile int num = 1;
    public static void main(String[] args) {
        new Thread(()->{
            for (;;){
                synchronized (lock){
                    try {
                        lock.notify();
                        System.out.println("基数: " + num + ",线程id:"+Thread.currentThread().getId());
                        num ++;
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(()->{
            for(;;){
                synchronized (lock) {
                    try {
                        lock.notify();
                        System.out.println("偶数: " + num + ",线程id:" + Thread.currentThread().getId());
                        num++;
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
